package com.hyst.web;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

@RestController
@RequestMapping("getQRCode")
// @CrossOrigin(maxAge = 3600, origins = "*")
// @Controller
public class ZxingEncoder {

	// private LogoConfig logiconfig ;
	// private ZxingDecoderService zxingDecodeService ;
	// private CoderService coderService ;
	//
	// @RequestMapping(value="/zxingdecode", method=RequestMethod.GET)
	// public ModelAndView zxingdecode(@RequestParam("realImgPath") String
	// realImgPath, HttpSession session) {
	// String uploadPath = "/images" ;
	// String realUploadPath =
	// session.getServletContext().getRealPath(uploadPath) ;
	// String imgPath = realUploadPath+"/"+realImgPath ;
	// String result = zxingDecodeService.zxingdecode(imgPath) ;
	//
	// ModelAndView ret = new ModelAndView() ;
	// ret.addObject("result", result) ;
	// ret.setViewName("zxingdecode");
	//
	// return ret ;
	// }

	/*
	 * @RequestMapping(value="/zxingcoder", method=RequestMethod.GET) public
	 * ModelAndView watermark(HttpSession session) throws Exception { String
	 * uploadPath = "/images" ; String realUploadPath =
	 * session.getServletContext().getRealPath(uploadPath) ; String imageName =
	 * "12345"+".png" ; // 模拟订单详情 String contents =
	 * "订单编号：20160512082345"+"\n"+"订单金额：￥ 2050.00"+"\n"+"支付方式：预存款"+"\n"+
	 * "配送方式：京东快递" ;
	 * 
	 * int width = 300; int height = 300; String zxingImage =
	 * coderService.encode(contents, width, height, uploadPath, realUploadPath,
	 * imageName);
	 * 
	 * File image = new File(realUploadPath+"/"+imageName); String logoImageUrl
	 * = logiconfig.LogoMatrix(image, uploadPath, realUploadPath, imageName) ;
	 * 
	 * ModelAndView ret = new ModelAndView() ; ret.addObject("imageUrl",
	 * zxingImage) ; ret.addObject("imageName", imageName) ;
	 * ret.addObject("logoImageUrl", logoImageUrl) ;
	 * ret.setViewName("zxingcoder");
	 * 
	 * return ret ; }
	 */

	// 2018-10-5 chenbin: 小程序接口：下载二维码
	@GetMapping("{studentId}")
	public void getQRCode(final HttpServletResponse resp, @PathVariable("studentId") final String id)
			throws IOException {
		final String url = "https://www.ejoyenglish.org/Index/index/orderaudition.html?recommenderId=" + id; // 2018-10-6
																												// chenbin:
																												// 二维码内容
		if (url != null && !"".equals(url)) {
			ServletOutputStream stream = null;
			try {
				final int width = 300;// 图片的宽度
				final int height = 300;// 高度
				stream = resp.getOutputStream();
				final QRCodeWriter writer = new QRCodeWriter();
				final BitMatrix m = writer.encode(url, BarcodeFormat.QR_CODE, height, width);
				MatrixToImageWriter.writeToStream(m, "png", stream);
				final Path path = new File("D:/zxingQRCode.png").toPath(); // 2018-10-5
																			// chenbin:
																			// 二维码下载路径
				MatrixToImageWriter.writeToPath(m, "png", path); // 2018-10-5
																	// chenbin:
																	// 下载二维码
			} catch (final WriterException e) {
				e.printStackTrace();
			} finally {
				if (stream != null) {
					stream.flush();
					stream.close();
				}
			}
		}
	}

	// 2018-10-6 chenbin: 待定
	// @RequestMapping(value="/download")
	// public ResponseEntity<byte[]> download(HttpServletRequest request,
	// @RequestParam("filename") String filename,
	// Model model)throws Exception {
	// //下载文件路径
	// String path = request.getServletContext().getRealPath("/images/");
	// File file = new File(path + File.separator + filename);
	// HttpHeaders headers = new HttpHeaders();
	// //下载显示的文件名，解决中文名称乱码问题
	// String downloadFielName = new
	// String(filename.getBytes("UTF-8"),"iso-8859-1");
	// //通知浏览器以attachment（下载方式）打开图片
	// headers.setContentDispositionFormData("attachment", downloadFielName);
	// //application/octet-stream ： 二进制流数据（最常见的文件下载）。
	// headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
	// return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
	// headers, HttpStatus.CREATED);
	// }

	// @Autowired
	// public void setLogiconfig(LogoConfig logiconfig) {
	// this.logiconfig = logiconfig;
	// }
	//
	// @Autowired
	// public void setZxingDecodeService(ZxingDecoderService zxingDecodeService)
	// {
	// this.zxingDecodeService = zxingDecodeService;
	// }
	//
	// @Autowired
	// public void setCoderService(CoderService coderService) {
	// this.coderService = coderService;
	// }

}
